from chinesepod import OAuthApi
import shelve
from lxml import objectify

# Get your key and secret at http://chinesepod.com/community/profile/create_application 
consumer_key = ''
consumer_secret = ''

if consumer_key == '' or consumer_secret == '':
    print """    Error: No consumer key or secret! 
    You must get your OAuth consumer key and secret by registering at
    http://chinesepod.com/community/profile/create_application
    and initializing the consumer_key/secret variables in the example.py code""" 
    import sys
    sys.exit()

tokens = shelve.open("tokens")

if not (tokens.has_key("oauth_access_token") and
        tokens.has_key("oauth_access_secret")):

    chinesepod = OAuthApi(consumer_key, consumer_secret)

    # Get the temporary credentials for our next few calls
    temp_credentials = chinesepod.getRequestToken()

    # Give the user an authorization URL to allow them to authorize this application
    print(chinesepod.getAuthorizationURL(temp_credentials))
    response = 'n'
    while response != 'y':
        response = raw_input("Did you authorize? (y/n)")

    access_token = chinesepod.getAccessToken(temp_credentials)

    print("oauth_token: " + access_token['oauth_token'])
    print("oauth_token_secret: " + access_token['oauth_token_secret'])

    tokens["oauth_access_token"] = access_token["oauth_token"]
    tokens["oauth_access_secret"] = access_token["oauth_token_secret"]

# Do a test API call using our new credentials
chinesepod = OAuthApi(consumer_key, consumer_secret,
                      tokens["oauth_access_token"],
                      tokens["oauth_access_secret"])

while True:
    action = raw_input("""What would you like to do?
            1) See some user details
            2) Look at the 20 latest lessons
            3) Add a word to your wordlist
            4) List the words in your flashcards
            q) Exit
             > """)

    if action == '1':
        user_settings = chinesepod.GetUserSettings()
        print "Username: ", user_settings.username
        print "user_id: ", user_settings.user_id
        print "Account type: ", user_settings.account_type_name
    elif action == '2':
        count = 0
        while count < 20:
            lessons = chinesepod.GetLatestLessons(offset = count)
            if len(lessons.lesson) == 0:
                break
            for lesson in lessons.lesson:
                print "%s is a podcast in the %s series (v3_id: %s)" % (lesson.title, lesson.channel_name, lesson.v3_id)
            count += 10
    elif action == '3':
        english_word = raw_input("Type the English word > ")
        chinese_word = raw_input("Type the Chinese word > ")
        chinesepod.AddWord(source=english_word, target=chinese_word)
    elif action == '4':
        word_list = chinesepod.GetWords()
        for word in word_list.word:
            print "(word_id %d) %s translates to %s" % (word.word_id.pyval, word.source, word.target)
    elif action == 'q':
        print "Quitting..."
        break
